﻿using System;
using System.Windows;
using DL.PhotoCollage.UI.Screensaver.Views;
using DL.PhotoCollage.Wpf.Presentation;

namespace DL.PhotoCollage.UI.Screensaver.Presentation
{
    internal class ScreensaverPresenter : CollagePresenterBase
    {      
        private bool hasMouseStartedMoving;

        private Point mousePosition;

        public ScreensaverPresenter(ControllerBase applicationController)
            : base(applicationController)
        {
            var view = new ScreensaverView(this);
            this.ViewWindow = view;
            this.ViewCanvas = view.MainCanvas;

            view.Show();
        }

        public void KeyDown()
        {
            this.Controller.ShutdownApplication();
        }

        public void MouseDown()
        {
            this.Controller.ShutdownApplication();
        }

        public void MouseMove(Point currentPosition)
        {
            try
            {
                if (!this.hasMouseStartedMoving)
                {
                    this.mousePosition = currentPosition;
                    this.hasMouseStartedMoving = true;
                }
                else
                {
                    if ((Math.Abs(this.mousePosition.X - currentPosition.X) > 10) ||
                        (Math.Abs(this.mousePosition.Y - currentPosition.Y) > 10))
                    {
                        this.Controller.ShutdownApplication();
                    }
                }
            }
            catch (Exception ex)
            {
                this.Controller.HandleError(ex, true);
                this.Controller.ShutdownApplication();
            }
        }
    }
}
